package com.scs.application.modules.sys.controller;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.sys.entity.RoleUser;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.sys.service.RoleUserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/roleuser")
public class RoleUserController extends CrudController<RoleUserService,RoleUser> {


    /**
     * 添加数据（通过userIds）
     * 参数1： String roleId
     * 参数2：List<String> userIds
     */
    @RequestMapping(value = "/saveByUserIds", method = RequestMethod.POST)
    public RestResult saveItem(@RequestBody Map<String, Object> para){
        String roleId = para.get("roleId").toString();
        String userIds = para.get("userIds").toString();
        boolean isOk = service.saveByUsers(roleId, userIds);
        if(!isOk){
            return RestResult.error("数据保存失败！");
        }
        return RestResult.ok("数据保存成功");
    }
    /**
     * 获取数据（通过roleId）
     * 参数1： String roleId
     */
    @RequestMapping(value = "/listByRoleId/{roleId}", method = RequestMethod.GET)
    public RestResult saveItem(@PathVariable(value="roleId") String roleId){
        List<RoleUser> models = service.getByRoleId(roleId);
        return RestResult.ok("获取角色用户成功！",models);
    }


    /**
     * 添加用户
     */
    @RequestMapping(value = "/addUserIds", method = RequestMethod.POST)
    public RestResult addUserIds(@RequestBody Map<String, Object> para){
        service.addUserIds(para.get("roleId")+"", para.get("userIds")+"");
        return RestResult.ok("用户添加成功");
    }

    /**
     * 添加删除用户
     */
    @RequestMapping(value = "/addOrDelete", method = RequestMethod.POST)
    public RestResult addOrDelete(@RequestBody Map<String, Object> para){
        service.addOrDelete(para.get("roleId")+"", para.get("addUserIds")+"", para.get("deleteUserIds")+"");
        return RestResult.ok("用户添加成功");
    }

}
